import 'package:flutter/material.dart';

class ExpansionPanelDemo extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => ExpansionPanelState();
}

class ExpansionPanelItem {
  String headerText;
  Widget body;
  bool isExpand;

  ExpansionPanelItem({this.headerText, this.body, this.isExpand});
}

class ExpansionPanelState extends State<ExpansionPanelDemo> {
  List<ExpansionPanelItem> expansionList;
  bool isexpand = false;

  @override
  void initState() {
    super.initState();

    expansionList = [
      ExpansionPanelItem(
          headerText: 'Panel A',
          body: Container(
            padding: EdgeInsets.all(16.0),
            width: double.infinity,
            child: Text('is panel a'),
          ),
          isExpand: false),
      ExpansionPanelItem(
          headerText: 'Panel B',
          body: Container(
            padding: EdgeInsets.all(16.0),
            width: double.infinity,
            child: Text('is panel b'),
          ),
          isExpand: false),
      ExpansionPanelItem(
          headerText: 'Panel C',
          body: Container(
            padding: EdgeInsets.all(16.0),
            width: double.infinity,
            child: Text('is panel c'),
          ),
          isExpand: false)
    ];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('ExpansionPanelDemo'),
        elevation: 0.0,
      ),
      body: Container(
        child: Container(
          padding: EdgeInsets.all(16.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              ExpansionPanelList(
                  expansionCallback: (int position, bool isExpand) {
                    setState(() {
                      expansionList[position].isExpand = !isExpand;
                    });
                  },
                  children: expansionList.map((ExpansionPanelItem item) {
                    return ExpansionPanel(
                        isExpanded: item.isExpand,
                        headerBuilder: (BuildContext context, bool isExpand) {
                          return Container(
                            padding: EdgeInsets.all(16.0),
                            child: Text(
                              item.headerText,
                              style: TextStyle(
                                fontSize: 16.0,
                              ),
                            ),
                          );
                        },
                        body: item.body);
                  }).toList())
            ],
          ),
        ),
      ),
    );
  }
}
